/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package api.purchase;

import Model.Purchase.PurchaseMaterielOfferList;
import Model.Purchase.PurchaseOfferList;
import Model.User.UserList;
import RssEasy.Core.DateTimeExtend;
import RssEasy.Core.Encrypt;
import RssEasy.Core.HttpRequestHelper;
import RssEasy.Core.ServletRequestHelper;
import RssEasy.Core.SignVerifyRequestHelper;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author zhangwenbo
 */
@WebServlet(name = "api_purchse_offer", urlPatterns = {"/api/purchase/offer"})
public class offer extends ServletRequestHelper { 
    @Override
    public HttpRequestHelper request(HttpServletRequest request, HttpServletResponse response) {
        super.request(request, response);
        try {
            PurchaseOfferList pol = new PurchaseOfferList(this.pageContext);
            pol.request();
            String c = this.get("counts");
            if (!this.get("myid").matches("^\\d{1,}")) {
                throw new Exception("100");
            }
            if (!this.get("purchaseid").matches("^\\d{1,}")) {
                throw new Exception("101");
            }
            if (pol.select("id").where("myid=? and purchaseid=?", this.get("myid"), this.get("purchaseid")).get_first_rows()) {
                int a = c.length();
                if (a == 1) {
                    pol.remove("counts");
                    pol.remove("price1").remove("freight1").remove("addr1").remove("icourl1").remove("materielid1").remove("taxpoint1").remove("spotgoods1").remove("enddt1");
                } else {
                    String[] b = c.split(",");
                    for (int i = 0; i < b.length; i++) {
                        pol.remove("counts").remove("price" + b[i] + "").remove("freight" + b[i] + "").remove("addr" + b[i] + "").remove("icourl" + b[i] + "").remove("materielid" + b[i] + "").remove("taxpoint" + b[i] + "").remove("spotgoods" + b[i] + "").remove("enddt" + b[i] + "");
                    }
                }
                pol.keyvalue("myid", this.get("myid")).keyvalue("purchaseid", this.get("purchaseid"));
                pol.timestamp();
                pol.update().where("myid=? and purchaseid=?", this.get("myid"), this.get("purchaseid")).submit();
            } else {
                int a = c.length();
                if (a == 1) {
                    pol.remove("counts").remove("price1").remove("freight1").remove("addr1").remove("icourl1").remove("materielid1").remove("taxpoint1").remove("spotgoods1").remove("enddt1");
                } else {
                    pol.remove("counts");
                    String[] b = c.split(",");
                    for (int i = 0; i < b.length; i++) {
                        pol.remove("price" + b[i] + "").remove("freight" + b[i] + "").remove("addr" + b[i] + "").remove("icourl" + b[i] + "").remove("materielid" + b[i] + "").remove("taxpoint" + b[i] + "").remove("spotgoods" + b[i] + "").remove("enddt" + b[i] + "");
                    }
                }
                pol.keyvalue("myid", this.get("myid")).keyvalue("purchaseid", this.get("purchaseid"));
                pol.timestamp();
                pol.append().submit();
            }
            
            PurchaseMaterielOfferList pmol = new PurchaseMaterielOfferList(this.pageContext);
            pmol.request();
            int a = c.length();
            if (a == 1) {
                if (pmol.select("myid").where("myid=? and materielid=?", this.get("myid"), this.get("materielid" + c + "")).get_first_rows()) {
                    pmol.keyvalue("price", this.get("price" + c + "")).keyvalue("freight", this.get("freight" + c + "")).keyvalue("addr", this.get("addr" + c + "")).keyvalue("spotgoods", this.get("spotgoods" + c + "")).keyvalue("icourl", this.get("icourl" + c + "")).keyvalue("enddt", this.get("enddt" + c + "")).keyvalue("taxpoint", this.get("taxpoint" + c + ""));
                    pmol.timestamp();
                    pmol.remove("counts").remove("price1").remove("freight1").remove("addr1").remove("icourl1").remove("materielid1").remove("taxpoint1").remove("spotgoods1").remove("enddt1");
                    pmol.update().where("myid=? and materielid=?", this.get("myid"), this.get("materielid" + c + "")).submit();
                } else {
                    pmol.keyvalue("myid", this.get("myid")).keyvalue("purchaseid", this.get("purchaseid")).keyvalue("price", this.get("price" + c + "")).keyvalue("freight", this.get("freight" + c + "")).keyvalue("addr", this.get("addr" + c + "")).keyvalue("spotgoods", this.get("spotgoods" + c + "")).keyvalue("icourl", this.get("icourl" + c + "")).keyvalue("materielid", this.get("materielid" + c + "")).keyvalue("enddt", this.get("enddt" + c + "")).keyvalue("taxpoint", this.get("taxpoint" + c + ""));
                    pmol.timestamp();
                    pmol.remove("counts").remove("price1").remove("freight1").remove("addr1").remove("icourl1").remove("materielid1").remove("taxpoint1").remove("spotgoods1").remove("enddt1");
                    pmol.append().submit();
                }
            } else {
                String[] b = c.split(",");
                for (int i = 0; i < b.length; i++) {
                    if (!this.get("price" + b[i] + "").matches("[0-9.]{1,}")) {
                        throw new Exception("102");
                    }
                    if (!this.get("freight" + b[i] + "").matches("[0-9.]{1,}")) {
                        throw new Exception("103");
                    }
                    if (!this.get("spotgoods" + b[i] + "").matches("^\\d{1}")) {
                        throw new Exception("105");
                    }
                    if (!this.get("materielid" + b[i] + "").matches("^\\d{1,}")) {
                        throw new Exception("107");
                    }
                    if (!this.get("taxpoint" + b[i] + "").matches("[0-9.]{1,10}")) {
                        throw new Exception("109");
                    }
                    if (pmol.select("myid").where("myid=? and materielid=?", this.get("myid"), this.get("materielid" + b[i] + "")).get_first_rows()) {
                        pmol.keyvalue("price", this.get("price" + b[i] + "")).keyvalue("freight", this.get("freight" + b[i] + "")).keyvalue("addr", this.get("addr" + b[i] + "")).keyvalue("spotgoods", this.get("spotgoods" + b[i] + "")).keyvalue("icourl", this.get("icourl" + b[i] + "")).keyvalue("enddt", this.get("enddt" + b[i] + "")).keyvalue("taxpoint", this.get("taxpoint" + b[i] + ""));
                        pmol.timestamp();
                        pmol.remove("counts");
                        String[] d = c.split(",");
                        for (int n = 0; n < d.length; n++) {
                            pmol.remove("price" + d[n] + "").remove("freight" + d[n] + "").remove("addr" + d[n] + "").remove("icourl" + d[n] + "").remove("materielid" + d[n] + "").remove("taxpoint" + d[n] + "").remove("spotgoods" + d[n] + "").remove("enddt" + d[n] + "");
                        }
                        pmol.update().where("myid=? and materielid=?", this.get("myid"), this.get("materielid" + b[i] + "")).submit();
                    } else {
                        pmol.keyvalue("myid", this.get("myid")).keyvalue("purchaseid", this.get("purchaseid")).keyvalue("price", this.get("price" + b[i] + "")).keyvalue("freight", this.get("freight" + b[i] + "")).keyvalue("addr", this.get("addr" + b[i] + "")).keyvalue("spotgoods", this.get("spotgoods" + b[i] + "")).keyvalue("icourl", this.get("icourl" + b[i] + "")).keyvalue("materielid", this.get("materielid" + b[i] + "")).keyvalue("enddt", this.get("enddt" + b[i] + "")).keyvalue("taxpoint", this.get("taxpoint" + b[i] + ""));
                        pmol.timestamp();
                        pmol.remove("counts");
                        String[] d = c.split(",");
                        for (int n = 0; n < d.length; n++) {
                            pmol.remove("price" + d[n] + "").remove("freight" + d[n] + "").remove("addr" + d[n] + "").remove("icourl" + d[n] + "").remove("materielid" + d[n] + "").remove("taxpoint" + d[n] + "").remove("spotgoods" + d[n] + "").remove("enddt" + d[n] + "");
                        }
                        pmol.append().submit();
                    }
                }
            }
            out.print("1");
        } catch (Exception e) {
            out.print("{\"rsscode\":" + e.getMessage() + "}");
        }
        return this;
    }
}

